﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"other.h"
using namespace std;
char uin[32] = { 0 };
struct info
{
    string token;
    string account;
    string qqkey;
    string skey;
    string p_skey;
};
string  ptsigx_url;
int main()
{
    info output;
    // 清理缓存
    DelTempFiles();
    // 初始化URL
    URL_COMPONENTSA crackedURL = { 0 };

    char URL_STRING[] = "https://ssl.xui.ptlogin2.weiyun.com/cgi-bin/xlogin?appid=527020901&daid=372&low_login=0&qlogin_auto_login=1&s_url=https://www.weiyun.com/web/callback/common_qq_login_ok.html?login_succ&style=20&hide_title=1&target=self&link_target=blank&hide_close_icon=1&pt_no_auth=1";
    char szHostName[128];
    char szUrlPath[256];
    crackedURL.dwStructSize = sizeof(URL_COMPONENTSA);
    crackedURL.lpszHostName = szHostName;
    crackedURL.dwHostNameLength = ARRAYSIZE(szHostName);
    crackedURL.lpszUrlPath = szUrlPath;
    crackedURL.dwUrlPathLength = ARRAYSIZE(szUrlPath);
    InternetCrackUrlA(URL_STRING, (DWORD)strlen(URL_STRING), 0, &crackedURL);

    // 初始化会话
    HINTERNET hInternet = InternetOpenA("Microsoft Internet Explorer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    HINTERNET hHttpSession = InternetConnectA(hInternet, crackedURL.lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    HINTERNET hHttpRequest = HttpOpenRequestA(hHttpSession, "GET", crackedURL.lpszUrlPath, NULL, "", NULL, INTERNET_FLAG_SECURE, 0);

    // 发送HTTP请求
    HttpSendRequest(hHttpRequest, NULL, 0, NULL, 0);

    // 查询HTTP请求状态
    DWORD dwRetCode = 0;
    DWORD dwSizeOfRq = sizeof(DWORD);
    BOOL bRet = FALSE;
    bRet = HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL);

    // 读取整个Headers
    char lpHeaderBuffer[1024] = { 0 };
    dwSizeOfRq = 1024;
    bRet = HttpQueryInfo(hHttpRequest, HTTP_QUERY_RAW_HEADERS, lpHeaderBuffer, &dwSizeOfRq, NULL);

    // 从Cookie中提取pt_local_token的值
    char* pt_local_token = lpHeaderBuffer + dwSizeOfRq;
    while (pt_local_token != lpHeaderBuffer)
    {
        if (strstr(pt_local_token, "pt_local_token="))
        {
            // 退出之前，修正偏移
            pt_local_token += sizeof("pt_local_token");
            char* pEndBuffer = strstr(pt_local_token, ";");
            *pEndBuffer = 0;
            break;
        }
        pt_local_token--;
    }

    //cout << "Token:" << pt_local_token << endl;
    output.token = pt_local_token;


    InternetCloseHandle(hHttpRequest);
    InternetCloseHandle(hHttpSession);


    time_t seed = time(NULL);
    srand((unsigned)seed);

    // 初始化URL参数
    char lpszUrlPath[1024] = { 0 };
    string randnum = GenerateRandomString(16).c_str();
    strcat_s(lpszUrlPath, "/pt_get_uins?callback=ptui_getuins_CB&r=0.");
    strcat_s(lpszUrlPath, randnum.c_str());            // 生成16位随机数
    strcat_s(lpszUrlPath, "&pt_local_tk=");
    strcat_s(lpszUrlPath, pt_local_token);    // 追加pt_local_token

    // cout << "URLPath:" << lpszUrlPath << endl;


     // 初始化会话
    hHttpSession = InternetConnectA(hInternet, "localhost.ptlogin2.weiyun.com", 4301, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    hHttpRequest = HttpOpenRequestA(hHttpSession, "GET", lpszUrlPath, NULL, "", NULL, INTERNET_FLAG_SECURE, 0);

    // 发送HTTP请求,添加头信息
    const char* lpHeaders = "Referer:https://ssl.xui.ptlogin2.weiyun.com/";
    HttpSendRequestA(hHttpRequest, lpHeaders, strlen(lpHeaders), NULL, 0);

    // 查询HTTP请求状态
    dwRetCode = 0;
    dwSizeOfRq = sizeof(DWORD);
    bRet = HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL);

    // 获取返回数据的大小
    DWORD dwNumberOfBytesAvailable = 0;
    bRet = InternetQueryDataAvailable(hHttpRequest, &dwNumberOfBytesAvailable, NULL, NULL);

    // 读取网页内容
    char* lpBuffer = new char[dwNumberOfBytesAvailable]();
    bRet = InternetReadFile(hHttpRequest, lpBuffer, dwNumberOfBytesAvailable, &dwNumberOfBytesAvailable);

    // 获取返回数据并提取uin（QQ号）

    findValueInJS(lpBuffer, "account", uin, sizeof(uin));
    //cout << " QQ:" << uin << endl;
    char L_uin[32] = { 0 };
    strcpy_s(L_uin, 32, uin);
    output.account = L_uin;

    // 释放资源,注意关闭句柄时按相反的顺序
    InternetCloseHandle(hHttpRequest);
    InternetCloseHandle(hHttpSession);

    /* 第三次会话 */

    // 初始化URL参数
    ZeroMemory(lpszUrlPath, MAX_PATH);
    strcat_s(lpszUrlPath, "/pt_get_st?clientuin=");
    strcat_s(lpszUrlPath, L_uin);
    strcat_s(lpszUrlPath, "&callback=ptui_getst_CB&pt_local_tk=");
    strcat_s(lpszUrlPath, pt_local_token);

    //  cout << "[+] URLPath:" << lpszUrlPath << endl;
      // 发送HTTPS请求

    hHttpSession = InternetConnectA(hInternet, "localhost.ptlogin2.weiyun.com", 4301, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    hHttpRequest = HttpOpenRequestA(hHttpSession, "GET", lpszUrlPath, NULL, "", NULL, INTERNET_FLAG_SECURE, 0);

    // 添加头信息
    lpHeaders = "Referer:https://ssl.xui.ptlogin2.weiyun.com/";
    HttpSendRequestA(hHttpRequest, lpHeaders, strlen(lpHeaders), NULL, 0);


    // 查询HTTP请求状态
    dwRetCode = 0;
    dwSizeOfRq = sizeof(DWORD);
    bRet = HttpQueryInfoA(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL);

    // 读取整个Headers
    ZeroMemory(lpHeaderBuffer, 1024);
    dwSizeOfRq = 1024;
    bRet = HttpQueryInfoA(hHttpRequest, HTTP_QUERY_RAW_HEADERS, lpHeaderBuffer, &dwSizeOfRq, NULL);

    // 从Cookie中提取ClientKey的值
    char* clientkey = lpHeaderBuffer + dwSizeOfRq;
    while (clientkey != lpHeaderBuffer)
    {
        if (strstr(clientkey, "clientkey="))
        {
            // 退出之前，修正偏移
            clientkey += sizeof("clientkey");
            char* pEndBuffer = strstr(clientkey, ";");
            *pEndBuffer = 0;
            break;
        }
        clientkey--;
    }

   // cout << "client key:" << clientkey << endl;
    output.qqkey = clientkey;
    InternetCloseHandle(hHttpRequest);
    InternetCloseHandle(hHttpSession);
    InternetCloseHandle(hInternet);
    /* 四次会话 */


    HINTERNET hInternet1 = InternetOpenA("Microsoft Internet Explorer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);//换个句柄
    // 构造 URL
    ZeroMemory(lpszUrlPath, MAX_PATH);
    strcat(lpszUrlPath, "/jump?clientuin=");
    strcat(lpszUrlPath, L_uin);
    strcat(lpszUrlPath, "&clientkey=");
    strcat(lpszUrlPath, clientkey);
    strcat(lpszUrlPath, "&keyindex=9&u1=https://www.weiyun.com/web/callback/common_qq_login_ok.html?login_succ&pt_local_tk=&pt_3rd_aid=0&ptopt=1&style=40");
    // 发送HTTPS请求
    hHttpSession = InternetConnectA(hInternet1, "ptlogin2.qq.com", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
   // cout << hHttpSession << endl;

    if (NULL != hHttpSession)
    {
        hHttpRequest = HttpOpenRequestA(hHttpSession, "GET", lpszUrlPath, NULL, "", NULL, INTERNET_FLAG_SECURE, 0);
        if (NULL != hHttpRequest)
        {
            // 添加Referer
            char lpReferer[128] = { 0 };
            strcpy(lpReferer, "Referer: ");
            strcat(lpReferer, "https://ptlogin2.qq.com/");
            strcat(lpReferer, "\r\n");

            HttpAddRequestHeaders(hHttpRequest, lpReferer, -1L, HTTP_ADDREQ_FLAG_ADD);

            bRet = HttpSendRequestA(hHttpRequest, NULL, NULL, NULL, 0);
            if (bRet)
            {
                // 查询HTTP请求状态
                dwRetCode = 0;
                dwSizeOfRq = sizeof(DWORD);
                bRet = HttpQueryInfoA(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL);
                if (bRet)
                {
                    // 获取返回数据的大小
                    DWORD dwNumberOfBytesAvailablex = 0;
                    InternetQueryDataAvailable(hHttpRequest, &dwNumberOfBytesAvailablex, NULL, NULL);

                    // 读取返回的 Response 数据
                    char* lpBufferx = new char[dwNumberOfBytesAvailablex + 1]();
                    InternetReadFile(hHttpRequest, lpBufferx, dwNumberOfBytesAvailablex, &dwNumberOfBytesAvailablex);


                    // 从返回数据中提取 ptsigx 
                    char* ptsigx = lpBufferx + dwNumberOfBytesAvailablex;
                    while (ptsigx != lpBufferx)
                    {
                        if (strstr(ptsigx, "check_sig?"))
                        {
                            ptsigx += sizeof("check_sig");
                            char* pEndBuffer = strstr(ptsigx, "'");
                            *pEndBuffer = 0;
                            break;
                        }
                        ptsigx--;
                    }
                    // 构造 ptsigx URL
                    string szPtsigx = "";
                    szPtsigx += "/check_sig?";
                    szPtsigx += ptsigx;

                   
                    ptsigx_url = szPtsigx;

                    delete[] lpBufferx;

                    // 读取整个Headers
                    char lpHeaderBuffer[1024] = { 0 };
                    dwSizeOfRq = 1024;
                    HttpQueryInfoA(hHttpRequest, HTTP_QUERY_RAW_HEADERS_CRLF, lpHeaderBuffer, &dwSizeOfRq, NULL);

                    // 提取 skey 的值
                    char* skey = lpHeaderBuffer + dwSizeOfRq;
                    while (skey != lpHeaderBuffer)
                    {
                        if (strstr(skey, "skey="))
                        {
                            skey += sizeof("skey");
                            char* pEndBuffer = strstr(skey, ";");
                            *pEndBuffer = 0;
                            break;
                        }
                        skey--;
                    }
                    // 关闭句柄
                    InternetCloseHandle(hHttpRequest);
                    InternetCloseHandle(hHttpSession);
                  //  cout << "Skey:" << skey << "\r\n" << endl;
                    output.skey = skey;
                }
            }
        } 
        InternetCloseHandle(hHttpRequest);
        InternetCloseHandle(hHttpSession);
        InternetCloseHandle(hInternet1);
       
        /* 第五次会话 */
        HINTERNET hInternet2 = InternetOpenA("Microsoft Internet Explorer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);//换个句柄
       

        // 发送HTTPS请求
        hHttpSession = InternetConnectA(hInternet2, "ssl.ptlogin2.weiyun.com", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
        if (NULL != hHttpSession)
        {
            hHttpRequest = HttpOpenRequestA(hHttpSession, "GET", ptsigx_url.c_str(), NULL, "", NULL, INTERNET_FLAG_SECURE, 0);
            if (NULL != hHttpRequest)
            {
                bRet = HttpSendRequestA(hHttpRequest, NULL, NULL, NULL, 0);
                if (bRet)
                {
                    // 查询HTTP请求状态
                    dwRetCode = 0;
                    dwSizeOfRq = sizeof(DWORD);
                    bRet = HttpQueryInfoA(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL);
                    if (bRet)
                    {
                        // 读取整个Headers
                        ZeroMemory(lpHeaderBuffer, 1024);
                        dwSizeOfRq = 1024;
                        HttpQueryInfoA(hHttpRequest, HTTP_QUERY_RAW_HEADERS_CRLF, lpHeaderBuffer, &dwSizeOfRq, NULL);

                       // cout << lpHeaderBuffer << endl;

                        // 提取 p_skey 的值
                        char* pskey = lpHeaderBuffer + dwSizeOfRq;
                        while (pskey != lpHeaderBuffer)
                        {
                            if (strstr(pskey, "p_skey="))
                            {
                                pskey += sizeof("p_skey");
                                char* pEndBuffer = strstr(pskey, ";");
                                *pEndBuffer = 0;
                                break;
                            }
                            pskey--;
                        }

                      // cout << " P_skey:" << pskey << "\r\n" << endl;
                        output.p_skey = pskey;
                    }
                }
            }
        }
        delete[] lpBuffer;
        std::ofstream outfile("data.txt");

        if (!outfile) {
            std::cerr << "无法打开data.txt";
            return 1; 
        }

        outfile << "QQ:" << output.account << endl
            <<"QQClientKey:"<<output.qqkey<< endl
            << "skey:"<<output.skey << endl
            << "p_skey: " << output.p_skey<<endl
            << "Token:" <<output.token<< endl;
          
        outfile.close();
        cout << "按下任意键继续...." << endl;
        getchar();
        system("start data.txt");
        return 0;
    }
}